/**
 * 公告按钮组件
 */
var NoticeBtnUnit = Unit.extend({
    _ccsFileName : res.NoticeBtnUnit_json,
    /**
     * 消息数据
     * @type {String}
     * @private
     */
    _noticeData: null,
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'btn_msg': '_openNotice'
        };
    },
    /**
     * 初始化
     */
    refreshData:function(){
        cc.log('refreshData');
        this._requestNoticeData();
        eventManager.addListener(EventKey.UpdateMessageNum, this._changeMessageNum.bind(this),this);
    },
    /**
     * 查看消息
     * @private
     */
    _openNotice: function () {
        if (!this._noticeData) {
            return;
        }
        showWindow('NoticeWindow', this._noticeData);
    },
    /**
     * 请求消息
     * @private
     */
    _requestNoticeData: function () {
        cc.log('_requestNoticeData');
        http.get(app.noticeUrl, this._requestNoticeDataSucess.bind(this));
    },
    /**
     *
     * @private
     */
    _requestNoticeDataSucess: function (data) {
        cc.log('_requestNoticeDataSucess');
        if (!data) {
            return;
        }
        var jsonData = JSON.parse(data);
        if (jsonData.result == 0) {
            this._noticeData = jsonData.data;
            this._autoShowNoticeWindow();
        }
        this._upDateMessageNum(this._noticeData)
    },
    /**
     * 自动弹出公告界面
     * @private
     */
    _autoShowNoticeWindow: function () {
        if (window.inReview || window.inBanShu) {
            return;
        }
        var lastShowDay = cc.sys.localStorage.getItem("lastShowDay");
        var nowDate = new Date().toDateString();
        if (nowDate != lastShowDay) {
            cc.sys.localStorage.setItem("lastShowDay", nowDate);
            showWindow('NoticeWindow', this._noticeData);
        }
    },
    /**
     * 更改未读数
     * @private
     */
    _changeMessageNum: function (num) {
        if (typeof (num) == "number") {
            safeString(this._nodeList["btn_msg.messagenum"],num);
            safeVisible(this._nodeList["btn_msg.messagenum"],(num != 0));
            safeVisible(this._nodeList["btn_msg.message_numbg"],(num != 0));
        }
    },
    /**
     * 设置公告未读数
     * @private
     */
    _upDateMessageNum: function (data) {
        var notReadNum = 0;
        for (var i = 0; i < data.length; i++) {
            var hasread = cc.sys.localStorage.getItem('notice' + data[i]['TimeInMillionSecond'] + gameData.uid);
            if (hasread == undefined || hasread == null || hasread == '') {
                notReadNum++;
            }
        }
        eventManager.dispatchEvent(EventKey.UpdateMessageNum, notReadNum);
    }
});